<?php
class FrontendController extends Module
{
    protected static  $type_frontend = 1;
    protected static  $hidden = 0;

    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
            'postOnly + delete', // we only allow deletion via POST request
        );
    }
    public function init()
    {

    }
    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules()
    {
        return array(
            array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions'=>array('index','main','singleArticle'),
                'users'=>array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array('*'),
                'users'=>array('@'),
            ),
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
                'actions'=>array('menu'),
                'roles'=>array('admin'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }
    //Генерируем меню сайта
    public function actionMain()
    {

        $cat = trim(Yii::app()->getRequest()->getParam('category'));
        //$menuItem = MenuItem::model()->findAllByAttributes(array('title'=>$view));
        $comm = Yii::app()->db->createCommand("SELECT id,title,type,params FROM {{menu_item}} WHERE link_path=:link_path AND status=1");
        $menuItem = $comm->bindParam(':link_path',$cat,PDO::PARAM_STR)->queryRow();


        if($menuItem['type'] === 'single-article')
        {
           // $model = Article::model()->findByPk($menuItem['params']);
            $sql = "SELECT a.id,a.title,a.content,a.create_time,u.name AS createUser FROM {{article}} AS a LEFT JOIN {{user}} AS u ON a.create_user_id=u.id  WHERE a.id=:id";
            $model = Yii::app()->db->createCommand($sql)->bindParam(':id',$menuItem['params'],PDO::PARAM_INT)->queryRow();

            if(!$model)
            {
                throw new CHttpException(404,'Page not found');
            }
            $this->render('single',array('model'=>$model));
        }
        if($menuItem['type'] === 'category-list')
        {
            $criteria = new CDbCriteria();
            $criteria->condition = 'status=1';
            $criteria->condition = 'category_id=:category_id';
            $criteria->params = array(':category_id'=>$menuItem['params']);

//          $count= Article::model()->count($criteria);
            $comm = Yii::app()->db->createCommand("SELECT COUNT(*) AS count FROM {{article}}  WHERE category_id=:category_id AND status=1");
            $comm->bindParam(':category_id',$menuItem['params'],PDO::PARAM_INT);
            $count = $comm->queryRow();
               // var_dump($count);exit;

            $pages=new CPagination($count['count']);
            $pages->pageSize=5;
            $pages->applyLimit($criteria);

            $model = Article::model()->findAll($criteria);
           // $comm = Yii::app()->db->createCommand("SELECT a.id,a.title,a.description,a.content,a.rating,a.category_id,a.tags,a.create_user_id,a.create_time,c.name AS categoryName,u.login AS author FROM {{article}} AS a LEFT JOIN {{category}} AS c ON a.category_id=c.id LEFT JOIN {{user}} AS u ON a.create_user_id=u.id WHERE a.category_id=:category_id AND a.status=1");
          //  $comm->bindParam(':category_id',$menuItem['params'],PDO::PARAM_INT);
           // $model = $comm->query()->readAll();
            $this->render('category_list',array(
                'model'=>$model,
                'pages'=>$pages,
            ));
        }
        if($menuItem['type'] === 'list-all-categories')
        {
            $model = Category::model()->findAll();
            $this->render('allcategories',array('model'=>$model));
        }
        if($menuItem['type'] === 'list-all-articles')
        {
            $model = Article::model()->findAll();
            $this->render('allarticles',array('model'=>$model));
        }

        if(!$menuItem)
        {
            throw new CHttpException(404,'Page not found');
        }
       // $this->render('single',array('model'=>$model));
    }
}
